#include "../header/common.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <fcntl.h>
#include <SDL/SDL_ttf.h>
#include <string.h>

#define MAXBALLS 2
#define MAXOBJECTS 6
#define MAXPOINTS 11
#define BUF_SIZ 4096

struct punktestand{
char zeit[80];
int punkte;
struct punktestand *next;
};

struct punktestand *first;


void ausgeben(SDL_Surface *screen)
{char string[200];
  TTF_Font *font4;
  int x=50,y=100;
  struct punktestand *helfer;
  helfer=first;

 font4=TTF_OpenFont("arial.ttf", 16);
  while (helfer !=NULL)
    {snprintf(string,sizeof(string),"Am %s haben sie ihren %d Punkt gemacht\n",helfer->zeit,helfer->punkte);
      drawText(screen, screen,font4,string ,x,y,0,0,0);
      printf("Am %s haben sie ihren %d Punkt gemacht\n",helfer->zeit,helfer->punkte); 
      helfer=helfer->next;y=y+16;
    }SDL_Flip(screen);	
}

void punkthinzu(int i)
{
  struct punktestand *neu;
  time_t t;
  struct tm *ts;
  char str[80];
  time(&t);
  strcpy(str,ctime(&t));
  
  if( first == NULL )
    {
      first = malloc( sizeof( *first ) );
      
      strcpy(first->zeit,str);
      first->punkte =i; 
      first->next = NULL;
    }
  else
    {
      neu = first;
      
      while( neu->next != NULL )
	neu = neu->next;
      
      neu->next = malloc( sizeof( *neu ) );
      neu = neu->next;
      strcpy(neu->zeit,str);
      neu->punkte = i;
      neu->next = NULL;
    }
}



int randombox2(SDL_Surface *screen,SDL_Rect box[],int *activebox,SDL_Rect obj[],SDL_Surface *barls,SDL_Surface *barrs, SDL_Surface *boxs, SDL_Rect field,int *killeractive,int *barspeed,int *maxballs)
{
  /*activebox=1 : Kommt von links,geht nach rechts
    activebox=2 : Kommt von rechts, geht nach links*/
      switch(*activebox)
	{
	case 1 :
	  if((box[0].x+box[0].w)<(obj[1].x+obj[1].w+10))
	    {
 	      SDL_FillRect(screen,&box[0],SDL_MapRGB(screen->format,255,255,255)); 
	      box[0].x+=(screen->w/100);
	      SDL_BlitSurface(boxs,NULL,screen,&box[0]);
	    }
	  else
	    {
	      if(((box[0].x+box[0].w)>=obj[1].x)&&(((box[0].y>obj[1].y)&&(box[0].y<(obj[1].y+obj[1].h)))||((box[0].y<obj[1].y)&&((box[0].y+box[0].h)>obj[1].y))))
		{
		  SDL_FillRect(screen,&obj[1],SDL_MapRGB(screen->format,255,255,255)); 
		  *activebox=2;		
		  return 2;
		}
	      *activebox=2;
	      SDL_FillRect(screen,&box[0],SDL_MapRGB(screen->format,255,255,255));
	      SDL_BlitSurface(barrs,NULL,screen,&obj[1]);
	      box[0].x=(obj[1].x-box[0].w);
	      box[0].y=((int)(field.y+field.h-box[0].h)*(rand()/(RAND_MAX+1.0)));
	      if(box[0].y<field.y)box[0].y+=field.y;
	    }
	  return 0;
	case 2 :
	  if(box[0].x>(obj[0].x-10))
	    {
	      SDL_FillRect(screen,&box[0],SDL_MapRGB(screen->format,255,255,255));
 	      box[0].x-=(screen->w/100);
	      SDL_BlitSurface(boxs,NULL,screen,&box[0]);
	    }
	  else
	    {
	      if((box[0].x<=(obj[0].x+obj[0].w))&&(((box[0].y>obj[0].y)&&(box[0].y<(obj[0].y+obj[0].h)))||((box[0].y<obj[0].y)&&((box[0].y+box[0].h)>obj[0].y))))
		{
		  SDL_FillRect(screen,&obj[0],SDL_MapRGB(screen->format,255,255,255)); 
		  *activebox=1;
		  return 1; 
		}
	      *activebox=1;
	      SDL_FillRect(screen,&box[0],SDL_MapRGB(screen->format,255,255,255));
	      SDL_BlitSurface(barls,NULL,screen,&obj[0]);
	      box[0].x=(obj[0].x+obj[0].w);
	      box[0].y=((int)(field.y+field.h-box[0].h)*(rand()/(RAND_MAX+1.0)));
	      if(box[0].y<field.y)box[0].y+=field.y;
	     
	    }
	  return 0;
	}
  return 0;
}

void sucheip(char ip[16])
{FILE *datei;
  char text[2000+1];
  char ipf[4]="127";
  int i,j,n=0,muh=0;
  
  /* ip wird mittels ifconfig in datei geschreiben und von dortaus selektiv ausgelesen */
  ip[0]=0;
  system ("ifconfig>>ip.tmp");
  datei = fopen ("ip.tmp", "r");
  if (datei != NULL)
    {
      fscanf (datei, "%2000c", text);
      
      text[2000] = '\0';
      fclose (datei);
    }
  for(i=0;i<2000;i++)
    {
      if ((text[i]=='i')&&(text[i+1]=='n')&&(text[i+2]=='e')&&(text[i+3]=='t')&&(text[i+4]==' '))
	{
	  for(j=i+13;j<i+28;j++)
	    {
	      ip[n]=text[j];
	      n++;
	    }
	  for(n=0;n<3;n++)
	    {
	      if(ip[n]!=ipf[n])muh=1;
	    }n=0;
	}if(muh)break;
    }
  for(i=0;i<16;i++)
    { 
      if(ip[i]==' ')ip[i]=0;
    }
  /*datei wird wieder geleert*/
  datei = fopen ("ip.tmp", "w");
  fclose (datei);
}



int server(SDL_Surface *screen,char name[50])
{
  SDL_Surface *balls, *barrs, *barls, *background, *boxs,*kreis,*warten,*button1,*button2,*fehler;
  SDL_Rect obj[MAXOBJECTS],ball[2],field,box[1],kreis1,kreis2,rect1,rect2,rect3;;
  SDL_Event event;
  int done=0,ballnumber,pointr=0,pointl=0,wait=0,sound,tmp,activebox=0,activeboxtmp=1,steptmp;
  int xway[2],yway[2],frames=0,step,level=10,autoaim=0,goboxtime=10,killeractive=0,maxballs=1;
  int x=0,infoplace,infozeile=2,font3high,autoaimtmp,go,xwaytmp=0,kill=0,ywaytmp=0,barspeed,points;
  int buffer1[21],buffer2[4],done2=0,s,click=0, c,bytes,i,muh=0,funzt,go1=0,go2=0,mode =3,sbox,forceeffect,aus=1;
  long unsigned int seconds=1,boxtime=time(NULL),pointtime;
  TTF_Font *font, *font2, *font3,*font4;
  char string[10],stringlong[35],pointstring[20];
  socklen_t addr_len;  
 struct sockaddr_in addr;
  struct hostent *he;
  struct in_addr **addr_list;
  char ip[16],name2[50];
  SDL_MouseMotionEvent mm;
  SDL_MouseButtonEvent mb;

  /*eigene ip wird herausgefunden */
sucheip(ip);
  TTF_Init();


  srand (time(NULL));

  /*Balkenpositionen werden festgelegt*/
  obj[0].x=50;obj[0].y=150;
  obj[1].x=((screen->w)-(screen->w/10));obj[1].y=150;
  field.x=(screen->w)/33;field.w=(screen->w)-(field.x*2);
  field.y=(screen->h)/5-((screen->w)/100);field.h=(screen->h)-((screen->h)/4)-((screen->w)/25);
  switch((screen->w))
    {
    case 640 :
      font = TTF_OpenFont("spranq_eco_sans_regular.ttf", 50);
      font2 =TTF_OpenFont("arial.ttf", 20);
      font3 = TTF_OpenFont("arial.ttf", 10);
      font4=TTF_OpenFont("arial.ttf", 16);      
      balls = SDL_LoadBMP("image/640480/ball.bmp");
      barrs = SDL_LoadBMP("image/640480/barr.bmp");
      barls = SDL_LoadBMP("image/640480/barl.bmp");
      background = SDL_LoadBMP("image/640480/background.bmp");
      boxs = SDL_LoadBMP("image/640480/box.bmp");
      warten=SDL_LoadBMP("image/640480/warten.bmp");
      button1=SDL_LoadBMP("image/640480/buttons/abbrechen1.bmp");
      button2=SDL_LoadBMP("image/640480/buttonspressed/abbrechen2.bmp");         
      kreis= SDL_LoadBMP("image/1024768/kreis.bmp");      
     fehler=SDL_LoadBMP("image/640480/fehler.bmp");
     rect1.x=160;rect1.y=120;  
      rect2.x=190;rect2.y=255;     
      rect3.x=175;rect3.y=195;
      kreis2.x=360;
      kreis2.y=180;
      kreis2.w=100;
      kreis2.h=100;       
      step=5;
      font3high=15;
      barspeed=15;
      break;

    case 800 :
      font = TTF_OpenFont("spranq_eco_sans_regular.ttf", 70);
      font2=TTF_OpenFont("arial.ttf", 30); 
      font3 = TTF_OpenFont("arial.ttf", 13);  
      font4=TTF_OpenFont("arial.ttf", 16);        
      balls = SDL_LoadBMP("image/800600/ball.bmp");
      barrs = SDL_LoadBMP("image/800600/barr.bmp");
      barls = SDL_LoadBMP("image/800600/barl.bmp");
      background = SDL_LoadBMP("image/800600/background.bmp");
      boxs = SDL_LoadBMP("image/800600/box.bmp");
       warten=SDL_LoadBMP("image/800600/warten.bmp");
      button1=SDL_LoadBMP("image/800600/buttons/abbrechen1.bmp");
      button2=SDL_LoadBMP("image/800600/buttonspressed/abbrechen2.bmp");         
      kreis= SDL_LoadBMP("image/1024768/kreis.bmp");
      fehler=SDL_LoadBMP("image/800600/fehler.bmp");   
      rect1.x=200;rect1.y=150;  
      rect2.x=238;rect2.y=319;     
      rect3.x=219;rect3.y=244;
      kreis2.x=450;
      kreis2.y=240;
      kreis2.w=100;
      kreis2.h=100;
      step=7;
      font3high=20;
      barspeed=23;
      break;

    case 1024 :
      font = TTF_OpenFont("spranq_eco_sans_regular.ttf", 90);
      font2=TTF_OpenFont("arial.ttf", 40);
      font3 = TTF_OpenFont("arial.ttf", 16);  
      font4=TTF_OpenFont("arial.ttf", 16);   
      balls = SDL_LoadBMP("image/1024768/ball.bmp");
      barrs = SDL_LoadBMP("image/1024768/barr.bmp");
      barls = SDL_LoadBMP("image/1024768/barl.bmp");
      background = SDL_LoadBMP("image/1024768/background.bmp");
      boxs = SDL_LoadBMP("image/1024768/box.bmp");
      warten=SDL_LoadBMP("image/1024768/warten.bmp");
      button1=SDL_LoadBMP("image/1024768/buttons/abbrechen1.bmp");
      button2=SDL_LoadBMP("image/1024768/buttonspressed/abbrechen2.bmp");         
      kreis= SDL_LoadBMP("image/1024768/kreis.bmp");      
      fehler=SDL_LoadBMP("image/1024768/fehler.bmp");      
      rect1.x=256;rect1.y=192;  
      rect2.x=305;rect2.y=408;     
      rect3.x=280;rect3.y=312;      
      kreis2.x=570;
      kreis2.y=300;
      kreis2.w=100;
      kreis2.h=100;      
      step=9;
      font3high=30;
      barspeed=30;
      break;
    }
  

  /* WARTEN AUF CLIENT FENSTER */

  s = socket(PF_INET, SOCK_STREAM, 0);
 
  if (s == -1)
    {
      perror("socket() failed");
        
    }
  
  addr.sin_addr.s_addr = INADDR_ANY;
  addr.sin_port = htons(8080);
  addr.sin_family = AF_INET;
  /* probiert socket zu binden und behandelt fehler*/
  if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) == -1)
    {
      perror("bind() failed");
      SDL_SetColorKey(fehler, SDL_SRCCOLORKEY, SDL_MapRGB(fehler->format, 0, 0, 255));
      SDL_BlitSurface(fehler,NULL,screen,&rect1);
	SDL_Flip(screen);
      SDL_Delay(5000);
	SDL_FreeSurface(background);
      SDL_FreeSurface(fehler);
      SDL_FreeSurface(barls);
      SDL_FreeSurface(barrs);
      SDL_FreeSurface(balls);
      SDL_FreeSurface(kreis);
      SDL_FreeSurface(warten);
      SDL_FreeSurface(button1);
      SDL_FreeSurface(button2);
      SDL_FreeSurface(boxs);
      return 0;
      
    }
  if (listen(s, 3) == -1)
    {
      perror("listen() failed");
        
    }
  /* zeichnet fenster */
SDL_SetColorKey(warten, SDL_SRCCOLORKEY, SDL_MapRGB(warten->format, 0, 0, 255));
 SDL_BlitSurface(warten,NULL,screen,&rect1);
 SDL_BlitSurface(button1,NULL,screen,&rect2);
 drawText(screen,screen,font3,ip,rect3.x,rect3.y,0,0,255);
 SDL_Flip(screen);
 
 addr_len = sizeof(addr);
 kreis1.x=0;
 kreis1.y=0;
 kreis1.w=100;
 kreis1.h=100;
 
 
 
  
 /* wartet auf client */
 fcntl(s, F_SETFL, O_NONBLOCK);
 while(!muh) 
   {    if(kreis1.x>700)kreis1.x=0;
     
	 SDL_BlitSurface(kreis,&kreis1,screen,&kreis2);
	 kreis1.x=kreis1.x+100;
	 
	 SDL_Delay(100);
	 SDL_Flip(screen);
	 c = accept(s, (struct sockaddr*)&addr, &addr_len);
	 if(c>0)muh=1;

	 while (SDL_PollEvent(&event)) 
	   { mm=event.motion;
	     click=click_button(rect2,mm.x,mm.y); 
	     switch(click)
	       {
	       case 0:
		 SDL_BlitSurface(button1,NULL,screen,&rect2);
		 break;
	       case 1:
		 SDL_BlitSurface(button2,NULL,screen,&rect2);
		 break;
	       }
	 switch(event.type) 
	   {
	   case SDL_QUIT:
	     muh=1;done = 1;
	     break;
	   case SDL_KEYDOWN:
	      switch(event.key.keysym.sym)
	       {
	       case SDLK_ESCAPE:
		 done=1;muh=1;
		 break;
	       }
	      
	   case SDL_MOUSEBUTTONDOWN:
	     mb=event.button;
	     click=click_button(rect2,mb.x,mb.y);
	     switch(click)
	       {
	       case 0:
		 break;
	       case 1:
		 muh=1;done=1;
		 break;
	       }
	    
	   } 
	 SDL_Flip(screen);
	 
       }
   }
 /* falls nicht vorzeitig abgebrochen wurde, wird server-seitiges spiel gestartet*/
 if(!done)
   {/*sockets werden wieder blockierend gesetzt*/

     fcntl(s, F_SETFL, !O_NONBLOCK);
     fcntl(c, F_SETFL, !O_NONBLOCK);



  
     /*spielfeld wird gezeichent */
  SDL_BlitSurface(background,NULL,screen,NULL);
  SDL_FillRect(screen,&field,SDL_MapRGB(screen->format,255,255,255));  

  steptmp=step;

  /*Box wird initialisiert*/
  box[0].w=boxs->w;box[0].h=boxs->h;
  box[0].x=(obj[0].x+(barls->w));
  box[0].y=((int)(field.y+field.h-box[0].h)*(rand()/(RAND_MAX+1.0)));
  if(box[0].y<field.y)box[0].y+=field.y;
 
 /* NAMEN WERDEN AUSGETAUSCHT UND GEBLITTET*/
  send(c,name,(strlen(name)),0);
  bytes=recv(c,name2,(sizeof(name2)),0);
  name2[bytes]=0;
  
 
  SDL_BlitSurface(background,NULL,screen,NULL);
  SDL_FillRect(screen,&field,SDL_MapRGB(screen->format,255,255,255));  
  drawText(screen,screen,font2,name2,((screen->w)/12),(screen->h/15)*14,0,0,0);
  drawText(screen,screen,font2,name,((screen->w)/15)*11,(screen->h/15)*14,0,0,0);

  for(ballnumber=0;ballnumber<2;ballnumber++)
    {
      /*Ballgeschwindigkeiten werden geladen*/
      xway[ballnumber]=1+(2*-ballnumber);yway[ballnumber]=1+(2*-ballnumber);
      /*Ballgroesse wird uebergeben*/
      ball[ballnumber].w = balls->w;
      ball[ballnumber].h = balls->h;
      /*Ballstartpositionen werden vergeben*/
      ball[ballnumber].x=(((screen->w)/2)+(balls->w*ballnumber*3));
      ball[ballnumber].y=(((screen->h)/2)-(balls->h*ballnumber*3));
    }

  /*Balken werden gezeichnet*/
  SDL_BlitSurface(barls,NULL,screen,&obj[0]);
  SDL_BlitSurface(barrs,NULL,screen,&obj[1]);

  infoplace=((screen->w/10));infozeile=0;
  drawText(screen,background,font3,"-------Info-------",infoplace,(infozeile*font3high+2),255,255,255);
  
  options(1,&sound,&tmp);
  if(sound==1)play(1);

  snprintf(string,sizeof(string),"%d",pointr);
  drawText(screen,background,font,string,(screen->w/100)*38 ,(screen->h/35),255,255,255);
  snprintf(string,sizeof(string),"%d",pointl);
  drawText(screen,background,font,string,(screen->w/100)*63,(screen->h/35),255,255,255);
  string[0]=0; 

  drawText(screen,background,font,"Ready!",((screen->w/2)-(screen->w/10)),((screen->h/2)-(screen->w/10)),255,0,0);
  SDL_Flip(screen);
  SDL_Delay(1000);
  drawText(screen,background,font,"Steady!",((screen->w/2)-(screen->w/10)),((screen->h/2)-(screen->w/10)),255,0,0);
  SDL_Flip(screen);
  SDL_Delay(1000);
  drawText(screen,background,font,"GO!!!      ",((screen->w/2)-(screen->w/10)),((screen->h/2)-(screen->w/10)),255,0,0);
  SDL_Flip(screen);
  SDL_Delay(500);
  drawText(screen,background,font,"                ",((screen->w/2)-(screen->w/10)),((screen->h/2)-(screen->w/10)),255,0,0);

  pointtime=time(NULL);
   }
 boxtime=time(NULL);
/*Programm Exit wird festgelegt*/    
 while ((!done)&&(!done2)) 
    {
      while (SDL_PollEvent(&event)) 
	{
	  switch(event.type) 
	    {
	    case SDL_QUIT:
	      done=1;
	      break;
	    }
	   
	}
      /*Balken werde geloescht*/
      

      /*Tastatur wird gecheckt*/
      autoaimtmp=autoaim;
SDL_FillRect(screen,&obj[0],SDL_MapRGB(screen->format,255,255,255));
      go1=keyhandler(obj,screen,&done,&mode,&autoaim,&barspeed);
      /* balken positionen werden ausgetauscht*/ 
      /*send(c,&obj[0],(sizeof(SDL_Rect)),0);
      bytes=recv(c,&obj[1],(sizeof(SDL_Rect)),0);
     */
    SDL_BlitSurface(barls,NULL,screen,&obj[0]);
      if(autoaim!=autoaimtmp)
	{
	  infozeile=3;
	  switch(autoaim)
	    {
	    case 0: drawText(screen,background,font3,"Autoaim:OFF",infoplace,(infozeile*font3high+2),255,255,255);
	      break;
	    case 1: drawText(screen,background,font3,"Autoaim:ON  ",infoplace,(infozeile*font3high+2),255,255,255);
	      break;
	    }
	}
      /*KI wird beim Singleplayer initialisiert*/
      if((mode==1)||(autoaim==1))ki(ball,obj,screen,&level,&autoaim,&mode);

      /*Balken werden geblittet*/
     	   
      
      /*Ballberechnung wird initialisiert*/
      for(ballnumber=0;ballnumber<maxballs;ballnumber++)
	{
	  SDL_FillRect(screen,&ball[ballnumber],SDL_MapRGB(screen->format,255,255,255));
	  xwaytmp=xway[0];
	  ywaytmp=yway[0];
 	  checkcollision(ball[ballnumber],screen,xway,yway,&ballnumber,&step);
	  if(killeractive!=0)
	    {
	      switch(killeractive)
		{
		case 1 :
		  if((xwaytmp<0)&&(xway[0]>0))step=steptmp*3;
		  if((xwaytmp>0)&&(xway[0]<0))step=steptmp;
		  break;
		case 2 :
		  if((xwaytmp>0)&&(xway[0]<0))step=steptmp*3;
		  if((xwaytmp<0)&&(xway[0]>0))step=steptmp;
		  break;
		}
	    }
	  else step=steptmp;
	  if((xwaytmp!=xway[0])||(ywaytmp!=yway[0]))kill++;
	  ball[ballnumber].x+=xway[ballnumber]*step;
	  ball[ballnumber].y+=yway[ballnumber]*step;
	  if(ball[ballnumber].x<(obj[0].x-10))
	    {
	      burn(ball[ballnumber],screen);
	      pointr++;
	      snprintf(string,sizeof(string),"%d",pointr);
	      drawText(screen,background,font,string,(screen->w/100)*63,(screen->h/35),255,255,255);	      
	      wait=1;
	    }
	  if(ball[ballnumber].x>(obj[1].x+10))
	    {
	      burn(ball[ballnumber],screen);
	      pointl++;	       
	      snprintf(string,sizeof(string),"%d",pointl);
	      drawText(screen,background,font,string,(screen->w/100)*38,(screen->h/35),255,255,255);
	      wait=1;
	      punkthinzu(pointl);
	    }	  
 	  if((pointl>MAXPOINTS)||(pointr>MAXPOINTS)){done=1;wait=0;} 
	  if(wait!=1)SDL_BlitSurface(balls,NULL,screen,&ball[ballnumber]);
	}

      /*Bilder pro Sekunde werden berechnet und ausgegeben*/
      if(seconds-time(NULL)>=1)
	{
	  seconds=time(NULL);
	  snprintf(string,sizeof(string),"%d FPS",frames);infozeile=1;
	  drawText(screen,background,font3,string,infoplace,(infozeile*font3high+2),255,255,255);
	  frames=0;

	  if(kill>(screen->w/100))
	    {
 	      wait=1;
	      SDL_FillRect(screen,&ball[0],SDL_MapRGB(screen->format,255,255,255));
	      SDL_FillRect(screen,&ball[1],SDL_MapRGB(screen->format,255,255,255));
	    }
	   kill=0;
	}   

      /*Das Spiel wartet 15ms um ca. 60FPS zu erreichen*/
      SDL_Delay(10);   


      /*Berechnung der "Special-Boxes"*/
      if((time(NULL)-boxtime)>goboxtime)
	{
	  goboxtime=(int)20*(rand()/(RAND_MAX+1.0));
	  if(goboxtime<10)goboxtime+=10;infozeile=2;
	  snprintf(stringlong,sizeof(stringlong),"Next box in %d s  ",goboxtime);
	  drawText(screen,background,font3,stringlong,infoplace,(infozeile*font3high+2),255,255,255);
	  boxtime=time(NULL);
	  activebox=activeboxtmp;
	  special(0,obj,barls,barrs,screen,&killeractive,&barspeed,&maxballs);
	}
      if(activebox!=0)
	{
	  activeboxtmp=activebox;
	 sbox=randombox2(screen,box,&activebox,obj,barls,barrs,boxs,field,&killeractive,&barspeed,&maxballs);
	}
      if((activebox!=activeboxtmp)&&(activebox!=0))
	{
	  activeboxtmp=activebox;
	  activebox=0;
	}

      /*random effect von box wird berechnet*/
      if(!sbox)
	{
	  forceeffect=(((int)14*(rand()/(RAND_MAX+1.0)))/2);
	  if(forceeffect<1)forceeffect=7;}
 x++;   

	      SDL_FillRect(screen,&obj[1],SDL_MapRGB(screen->format,255,255,255));     
if(x==3)
      {
/* kommunikation zwischen server und client*/
      buffer1[0]=ball[0].x;
      buffer1[1]=ball[0].y;
      buffer1[2]=ball[0].w;
      buffer1[3]=ball[0].h;
      buffer1[4]=done;
      buffer1[5]=go1;
      buffer1[6]=sbox;
      buffer1[7]=forceeffect;
      buffer1[8]=maxballs;
      buffer1[9]=ball[1].x;
      buffer1[10]=ball[1].y;
      buffer1[11]=ball[1].w;
      buffer1[12]=ball[1].h;
      buffer1[13]=box[0].x;
      buffer1[14]=box[0].y;
      buffer1[15]=box[0].w;
      buffer1[16]=box[0].h;
      buffer1[17]=activebox;
      buffer1[18]=goboxtime;
buffer1[19]=obj[0].x;  
buffer1[20]=obj[0].y; 

    
send(c,buffer1,21*(sizeof(int)),0);
      bytes=recv(c,buffer2,4*(sizeof(int)),0);
      if(bytes >0)
	{
	  done2=buffer2[0];
	  go2=buffer2[1];  
obj[1].x=buffer2[2];	
obj[1].y=buffer2[3];

	}      

	x=0;}
	
 SDL_BlitSurface(barrs,NULL,screen,&obj[1]);	   
      
   /* boxevent wird ausgeführt*/
      if(sbox==1)
	{
	  
	  isomorph(obj[0],screen,barls,&killeractive,&barspeed,&maxballs,&forceeffect);
	  sbox=0;
	  SDL_FillRect(screen,&box[0],SDL_MapRGB(screen->format,255,255,255));
	  SDL_BlitSurface(barls,NULL,screen,&obj[0]);
	  box[0].x=(obj[0].x+obj[0].w);
	  box[0].y=((int)(field.y+field.h-box[0].h)*(rand()/(RAND_MAX+1.0)));
	  if(box[0].y<field.y)box[0].y+=field.y;
	}
      if(sbox==2)
	{
	  
	  isomorph(obj[1],screen,barrs,&killeractive,&barspeed,&maxballs,&forceeffect);
	  sbox=0;
	  SDL_FillRect(screen,&box[0],SDL_MapRGB(screen->format,255,255,255));
	  SDL_BlitSurface(barrs,NULL,screen,&obj[1]);
	  box[0].x=(obj[1].x-box[0].w);
	  box[0].y=((int)(field.y+field.h-box[0].h)*(rand()/(RAND_MAX+1.0)));
	  if(box[0].y<field.y)box[0].y+=field.y;
	}
      /*chat wird gestartet*/
      if((go2==-1)|(go1==-1))
	{
	  SDL_Delay(100);
	  chat(screen,c,name);
	  fcntl(c,F_SETFL,!O_NONBLOCK);
	  SDL_Delay(400);
	  }
      /*Die Bildschirm-Surface wird "gewendet"*/
      SDL_Flip(screen);

      if(wait==1)
	{
	  for(ballnumber=0;ballnumber<maxballs;ballnumber++)
	    {
	      SDL_FillRect(screen,&ball[ballnumber],SDL_MapRGB(screen->format,255,255,255));
	      ball[ballnumber].x=(((screen->w)/2)+((ballnumber*3)*(balls->w)));
	      ball[ballnumber].y=(((screen->h)/2)+((ballnumber*3)*(balls->h)));
	      if(((int)100*(rand()/(RAND_MAX+1.0)))<50)xway[ballnumber]=-1;else xway[ballnumber]=1;
	      if(((int)100*(rand()/(RAND_MAX+1.0)))<50)yway[ballnumber]=-1;else yway[ballnumber]=1;
	    }
	  drawText(screen,background,font,"GO!!!",((screen->w/2)-(screen->w/10)),((screen->h/2)-(screen->w/10)),255,0,0);
	  SDL_Flip(screen);
	  SDL_Delay(300);
	  drawText(screen,background,font,"          ",((screen->w/2)-(screen->w/10)),((screen->h/2)-(screen->w/10)),255,0,0);
	  step=steptmp;
	  wait=0;
	  maxballs=1;
	}
      /*Frames werden hochgezaehlt*/
      frames++;
    } 

  /*Die Surfaces werden wieder geloescht*/
  SDL_FreeSurface(balls);
  SDL_FreeSurface(barls);
  SDL_FreeSurface(barrs); 
  SDL_FreeSurface(warten); 
  SDL_FreeSurface(button1); 
  SDL_FreeSurface(button2);
  SDL_FreeSurface(kreis);  
  play(0);
  


  /*Darstellung des Gewinners*/
  if((pointl>MAXPOINTS)||(pointr>MAXPOINTS))
    {
      /*Berechnung der Punkte*/
      if(mode==1)
	{
	  points=((600-(time(NULL)-pointtime)-pointr)*(level+1));
	  snprintf(pointstring,sizeof(pointstring),"with %d points!!!",points);
	}
      drawText(screen,background,font,"And the winner is...",80,((screen->h/2)-((screen->w/10)*2)),255,0,0);
      SDL_Flip(screen);
      SDL_Delay(1000);
      if(pointl<pointr)
	{
	  if(mode==1)drawText(screen,background,font,"Mr. Computer!",80,((screen->h/2)-(screen->w/10)),255,0,0);
	  if(mode==2)drawText(screen,background,font,"The RIGHT player!",80,((screen->h/2)-(screen->w/10)),255,0,0);
	}
      else
	{
	  if(mode==1)
	    {
	      drawText(screen,background,font,name,screen->w/3,((screen->h/2)-(screen->w/10)),255,0,0);
	      drawText(screen,background,font,pointstring,screen->w/6,((screen->h/3)*2),255,0,0);
	    }
	  if(mode==2)drawText(screen,background,font,"The LEFT player!",80,((screen->h/2)-(screen->w/10)),255,0,0);
	}
      
      SDL_Flip(screen);
   /*   highscore(1,screen,playername,points);*/
      SDL_Delay(3000);
    }
  
  SDL_FreeSurface(background);
  /* sockets werden wieder geschlossen*/ 
   close(c);
  close(s);
 
 
   return 0;
 
}
